//
//  YunCangViewController.swift
//  Wmsmobile
//
//  Created by wangshuning on 16/7/19.
//  Copyright © 2016年 Nick xu. All rights reserved.
//

import UIKit

import SVProgressHUD

let  YunCangCollectionSpace:CGFloat = 1

class YunCangViewController: UIViewController, mainMenuSlideViewDelegate ,UIScrollViewDelegate{
    //MARK: - property
    var scrollView: UIScrollView? 
    var slidView : mainMenuSlideView?
    //MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        let scroll = UIScrollView(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight))//添加scrollView
        self.view.addSubview(scroll)
        scroll.bounces = false
        scrollView = scroll
        scroll.backgroundColor = UIColor.white
        scroll.delegate = self
        scroll.contentSize = CGSize(width: KscreenWidth * 2, height: 0)
        scroll.isPagingEnabled = true
        let slideView = mainMenuSlideView(frame: CGRect(x: 0, y: 0, width: 125, height: 44))
        slidView = slideView
        slideView.delegate = self
        self.navigationItem.titleView = slideView
        scroll.addSubview(YunCangView(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight - 64), collectionViewLayout: UICollectionViewLayout()))
        
        let view2 = FavoriteView()
        view2.frame = CGRect(x: KscreenWidth, y: 0, width: KscreenWidth, height: KscreenHeight - 64)
        scrollView?.addSubview(view2)
    }
    override func viewDidAppear(_ animated: Bool) {
        AssistBatterQuery.queryBatteryInfo()
    }
    //MARK: - collectionViewDelegate
    
    //MARK: - delegate
    
    func didClickButton(_ button: UIButton) {
        if self.scrollView?.contentOffset.x  == KscreenWidth {
            UIView.animate(withDuration: 0.5, animations: {
                self.scrollView?.contentOffset.x = 0
            })
        }else{
            UIView.animate(withDuration: 0.5, animations: {
                self.scrollView?.contentOffset.x = KscreenWidth
            })
        }
   }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if self.scrollView?.contentOffset.x == 0 {
           self.slidView?.moveButton((slidView?.leftButton)!)
        }else{
         self.slidView?.moveButton((slidView?.rightButton)!)
        }
    }
    
    func updateDatasource(_ array: NSArray) {
   }
}


//MARK: -
//MARK: - 头部自定义选择器view
protocol mainMenuSlideViewDelegate:NSObjectProtocol{
    func didClickButton(_ button:UIButton)
}

class mainMenuSlideView:UIView {
    //MARK: - property
    let colorSelected = UIColor.hexStringToUIColor("#00acff")
    let whiteColor = UIColor.white
    var activeButton:UIButton?
    var disableButton:UIButton?
    let indectorViewHeight:CGFloat = 4
    weak var delegate:mainMenuSlideViewDelegate?
    //MARK: - life cycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(leftButton)
        self.addSubview(rightButton)
//        self.addSubview(indectorView)
        self.activeButton = leftButton
        self.disableButton = rightButton
        leftButton.setTitleColor(colorSelected, for: UIControlState())
        leftButton.setBackgroundImage(UIImage.init(named: "滑动左hover"), for: UIControlState())
        rightButton.setTitleColor(whiteColor, for: UIControlState())
        rightButton.setBackgroundImage(UIImage.init(named: "滑动右home"), for: UIControlState())
        self.leftButton.frame = CGRect(x: 0, y: 3, width: frame.size.width / 2+1, height: frame.size.height - 10)
        self.rightButton.frame = CGRect(x: frame.size.width / 2, y: 3, width: frame.size.width / 2, height: frame.size.height - 10)
//        self.indectorView.frame = CGRectMake(0, frame.size.height - indectorViewHeight,62.5 , indectorViewHeight)
//        indectorView.backgroundColor = colorSelected
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - UIResponder
    
    func moveButton(_ button:UIButton){
        if button == rightButton {
            leftButton.setBackgroundImage(UIImage.init(named: "滑动左home"), for: UIControlState())
             leftButton.setTitleColor(whiteColor, for: UIControlState())
            rightButton.setBackgroundImage(UIImage.init(named: "滑动右hover"), for: UIControlState())
            rightButton.setTitleColor(colorSelected, for: UIControlState())

        }else{
            leftButton.setBackgroundImage(UIImage.init(named: "滑动左hover"), for: UIControlState())
            leftButton.setTitleColor(colorSelected, for: UIControlState())
            rightButton.setTitleColor(whiteColor, for: UIControlState())
            rightButton.setBackgroundImage(UIImage.init(named: "滑动右home"), for: UIControlState())
            
        }
//        if activeButton == button {
//            return
//        }
//        activeButton?.setTitleColor(greenColor, forState: UIControlState.Normal)
//        activeButton!.setImage(UIImage.init(named: "滑动左home"), forState: UIControlState.Normal)
//        button.setImage(UIImage.init(named: "滑动右hover"), forState: UIControlState.Normal)
//        button.setTitleColor(colorSelected, forState: UIControlState.Normal)
////        leftButton.setImage(UIImage.init(named: "滑动左hover"), forState: UIControlState.Normal)
////        rightButton.setImage(UIImage.init(named: "滑动右home"), forState: UIControlState.Normal)
//        activeButton = button
//        UIView.animateWithDuration(0.5) { //移动指示器
////            self.indectorView.centerX = button.titleForState(UIControlState.Normal) == "收藏" ? self.rightButton.centerX : self.leftButton.centerX
//        }
    }

    func clickButton(_ button:UIButton){
        if button == rightButton {
            leftButton.setBackgroundImage(UIImage.init(named: "滑动左home"), for: UIControlState())
            leftButton.setTitleColor(whiteColor, for: UIControlState())
            rightButton.setBackgroundImage(UIImage.init(named: "滑动右hover"), for: UIControlState())
            rightButton.setTitleColor(colorSelected, for: UIControlState())
            
        }else{
            leftButton.setBackgroundImage(UIImage.init(named: "滑动左hover"), for: UIControlState())
            leftButton.setTitleColor(colorSelected, for: UIControlState())
            rightButton.setTitleColor(whiteColor, for: UIControlState())
            rightButton.setBackgroundImage(UIImage.init(named: "滑动右home"), for: UIControlState())
            
        }

//        if activeButton == button {
//            return
//        }
//        activeButton?.setTitleColor(whiteColor, forState: UIControlState.Normal)
//        button.setTitleColor(colorSelected, forState: UIControlState.Normal)
////        button.setImage(UIImage.init(named: "滑动右hover"), forState: UIControlState.Normal)
////        activeButton!.setImage(UIImage.init(named: "滑动左home"), forState: UIControlState.Normal)
        activeButton = button
        if self.delegate != nil{
            self.delegate?.didClickButton(button)
        }
//        UIView.animateWithDuration(0.5) { //移动指示器
////            self.indectorView.centerX = button.titleForState(UIControlState.Normal) == "收藏" ? self.rightButton.centerX : self.leftButton.centerX
//        }
    }
    
    //MARK: - lazy loading
    lazy var leftButton:UIButton = {
        let bt = UIButton()
        bt.setTitle("菜单", for: UIControlState())
        bt.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        bt.addTarget(self, action:#selector(mainMenuSlideView.clickButton(_:)), for: UIControlEvents.touchUpInside)
        return bt
    }()
    lazy var rightButton:UIButton = {
        let bt = UIButton()
        bt.setTitle("收藏", for: UIControlState())
        bt.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        bt.addTarget(self, action:#selector(mainMenuSlideView.clickButton(_:)), for: UIControlEvents.touchUpInside)
        return bt
    }()
//    lazy private var indectorView:UIView = {
//        let temp = UIView()
////        temp.backgroundColor = colorSelected
//        return temp
//    }()
}












